package com.zwj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zwj.bo.StudentCourseBo;
import com.zwj.dto.PaginationDate;
import com.zwj.dto.Result;
import com.zwj.dto.ReturnDate;
import com.zwj.dto.StudentCourse;
import com.zwj.entity.CourseEntity;
import com.zwj.entity.StudentCourseEntity;
import com.zwj.entity.StudentEntity;
import com.zwj.mapper.CourseMapper;
import com.zwj.mapper.StudentCourseMapper;
import com.zwj.mapper.StudentMapper;
import com.zwj.service.StudentCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.utils.PaginationUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-10-30
 */
@Service
public class StudentCourseServiceImpl extends ServiceImpl<StudentCourseMapper, StudentCourseEntity> implements StudentCourseService {
    private final StudentMapper studentMapper;
    private final CourseMapper courseMapper;
    private final StudentCourseMapper studentCourseMapper;

    public StudentCourseServiceImpl(StudentMapper studentMapper,StudentCourseMapper studentCourseMapper,CourseMapper courseMapper) {
        this.studentMapper = studentMapper;
        this.studentCourseMapper = studentCourseMapper;
        this.courseMapper = courseMapper;
    }
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public Result addStudentCourse(StudentCourseBo studentCourseBo) {
        //数据校验
        if(studentCourseBo.getCourseId()==null||studentCourseBo.getCourseId().length ==0){
            return Result.error().message("课程ID不能为空");
        }
        if(studentCourseBo.getStudentId()==null||studentCourseBo.getStudentId().equals("")){
            return Result.error().message("学生ID不能为空");
        }
        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(StudentEntity::getId,studentCourseBo.getStudentId()).eq(StudentEntity::getIsDeleted,"0");
        StudentEntity studentEntity = studentMapper.selectOne(wrapper);
        if (studentEntity== null) {
            return Result.error().message("学生不存在");
        }
        StudentCourseEntity studentCourseEntity = new StudentCourseEntity();
        studentCourseEntity.setStudentId(studentCourseBo.getStudentId());
        for(String courseId:studentCourseBo.getCourseId()){
            QueryWrapper<CourseEntity> wrapper1 = new QueryWrapper<>();
            wrapper1.lambda().eq(CourseEntity::getId,courseId).eq(CourseEntity::getIsDeleted,"0");
            CourseEntity courseEntity = courseMapper.selectOne(wrapper1);
            if(courseEntity!=null){
                studentCourseEntity.setCourseId(courseId);
                int row = studentCourseMapper.insert(studentCourseEntity);
                if(row == 0){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return Result.error().message("课程ID不存在");
                }
            }
        }
        return Result.ok();
    }

    @Override
    public Result seleteStudentCourse(StudentCourseBo studentCourseBo) {
        //数据校验s
        if(studentCourseBo.getStudentId()==null||studentCourseBo.getStudentId().equals("")){
            return Result.error().message("学生ID不能为空");
        }
        List<StudentCourse> studentCourse = studentCourseMapper.seleteStudentCourse(studentCourseBo);
        if (studentCourse.size() == 0) return Result.error().message("查询结果为空");
        return Result.ok().data(studentCourse);
    }
}
